﻿using System;
using System.IO;
using System.Windows.Forms;

namespace FileExplorer.view
{
	/*
	 * List all sub-directory of current directory
	 * Identify all string template by string pattern which user input.
	 * 
	 */

	public partial class ListFile : Form
	{
		protected string sKey;

		public ListFile()
		{
			InitializeComponent();
			sKey = "function"; // string.Empty;
		}

		private void btnDirectoryBrowser_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
			folderBrowser.ShowNewFolderButton = false;
			folderBrowser.RootFolder = Environment.SpecialFolder.Personal;

			string folderName = string.Empty;

			if (DialogResult.OK == folderBrowser.ShowDialog() && string.Empty != folderBrowser.SelectedPath)
			{
				folderName = folderBrowser.SelectedPath;
				txtDirectoryPath.Text = folderName;

				browseDirectory(folderName);
			}
		}

		private bool browseDirectory(string folderPath)
		{
			bool bResult = false;

			rtbResult.Text = string.Empty;

			if (Directory.Exists(folderPath))
			{
				string[] filePaths = Directory.GetDirectories(folderPath);
				if (filePaths.Length > 0)
				{
					foreach (string item in filePaths)
					{
						rtbResult.AppendText(item.Substring(item.LastIndexOf('\\') + 1, item.Length - item.LastIndexOf('\\') - 1) + "\n");
					}
				}

				filePaths = Directory.GetFiles(folderPath);
				if (filePaths.Length > 0)
				{
					foreach (string item in filePaths)
					{
						rtbResult.AppendText(item.Substring(item.LastIndexOf('\\') + 1, item.Length - item.LastIndexOf('\\') - 1) + "\n");
					}
				}

				bResult = true;
			}
			else
			{
				rtbResult.Text = "Not right path";
			}

			return bResult;
		}

		private void btnFileBrowser_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();

			openFileDialog.Filter = "All Files (*.*)|*.*";
			openFileDialog.FilterIndex = 1;
			openFileDialog.Multiselect = false;

			// Process input if the user clicked OK.
			if ((openFileDialog.ShowDialog() == DialogResult.OK) && (string.Empty != openFileDialog.FileName))
			{
				//show file path on text box
				txtFilePath.Text = openFileDialog.FileName;

				browseFile(openFileDialog.FileName);
			}
		}

		private bool browseFile(string filePath)
		{
			bool bResult = false;
			string line = string.Empty;
			string keyString = string.Empty;

			rtbResult.Text = string.Empty;

			if (File.Exists(filePath))
			{
				// Open the selected file to read.
				using (StreamReader reader = new StreamReader(filePath))
				{
					// Read and display lines from the file until the end of 
					// the file is reached.
					while ((line = reader.ReadLine()) != null)
					{
						if (line.Contains(sKey))
						{
							keyString = line.Substring(line.LastIndexOf(sKey) + sKey.Length + 1, line.IndexOf('(') - (line.LastIndexOf(sKey) + sKey.Length + 1));
							rtbResult.AppendText(keyString + "\n");
						}
					}
				}
				bResult = true;
			}
			else
			{
				rtbResult.Text = "Not right path";
			}

			return bResult;
		}

		private void txtDirectoryPath_Enter(object sender, EventArgs e)
		{
			browseDirectory(txtDirectoryPath.Text);
		}

		private void txtFilePath_Enter(object sender, EventArgs e)
		{
			browseFile(txtFilePath.Text);
		}

		private void rtbResult_Resize(object sender, EventArgs e)
		{
			if (FormWindowState.Minimized == WindowState)
				Hide();
		}

		private void nIcon_DoubleClick(object sender, EventArgs e)
		{
			Show();
			WindowState = FormWindowState.Normal;
		}
	}
}
